package com.cap.dreamcircle.View.Adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cap.dreamcircle.Model.Bean.GridCalenderDateEntity;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.DisplayUtil;
import com.cap.dreamcircle.Utils.KeyCounterUtils;
import com.cap.dreamcircle.Utils.PreferenceUtils;
import com.cap.dreamcircle.Utils.ViewHolderUtils;
import com.cap.dreamcircle.View.PersonalInfoActivity;
import com.cap.dreamcircle.View.RecordDreamTitleActivcity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created by liucxu on 2017/9/1.
 */

public class CalendarGridViewAdapter extends BaseAdapter {

    private List<GridCalenderDateEntity> datas = new ArrayList<>();
    private Context mContext;

    public CalendarGridViewAdapter(Context c, List<GridCalenderDateEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            mContext = c;
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_grid_calendar, null);
        }
        initViews(position, convertView);
        return convertView;
    }

    private void initViews(int position, View convertView) {
        final GridCalenderDateEntity entity = datas.get(position);
        RelativeLayout layout_calendar = ViewHolderUtils.get(convertView, R.id.layout_calendar);
        TextView tv_calendar_date = ViewHolderUtils.get(convertView, R.id.tv_calendar_date);
        ViewGroup.LayoutParams para;
        para = layout_calendar.getLayoutParams();
        para.height = DisplayUtil.GetDisplayWidth() / 7;
        para.width = DisplayUtil.GetDisplayWidth() / 7;
        layout_calendar.setLayoutParams(para);
        if (entity.isCurrentMonth()) {
            tv_calendar_date.setTextColor(Color.parseColor("#ffffff"));
        } else {
            tv_calendar_date.setTextColor(Color.parseColor("#344258"));
        }
        tv_calendar_date.setText(entity.getDay() + "");
        if (entity.getDreamType() == -1) {
            tv_calendar_date.setBackgroundResource(R.drawable.bg_no_dream_circle);
        }
        //好梦
        else if (entity.getDreamType() == 1) {
            tv_calendar_date.setBackgroundResource(R.mipmap.good_normal_bad_dreams_bg);
        }
        //怀梦
        else if (entity.getDreamType() == 2) {
            tv_calendar_date.setBackgroundResource(R.mipmap.bad_good_dreams_bg);
        }
        //一般梦
        else if (entity.getDreamType() == 3) {
            tv_calendar_date.setBackgroundResource(R.mipmap.bad_normal_dreams_bg);
        } else if (entity.getDreamType() == 4) {
            tv_calendar_date.setBackgroundResource(R.mipmap.good_normal_dreams_bg);
        } else if (entity.getDreamType() == 5) {
            tv_calendar_date.setBackgroundResource(R.drawable.bg_bad_dream_circle);
        } else if (entity.getDreamType() == 6) {
            tv_calendar_date.setBackgroundResource(R.drawable.bg_normal_dream_circle);
        } else if (entity.getDreamType() == 7) {
            tv_calendar_date.setBackgroundResource(R.drawable.bg_good_dream_circle);
        }
        if (Calendar.getInstance().get(Calendar.YEAR) < entity.getYear()) {
            convertView.setEnabled(false);
        } else if ((Calendar.getInstance().get(Calendar.YEAR) - entity.getYear() == 0) && ((Calendar.getInstance().get(Calendar.MONTH) + 1) < (entity.getMonth()))) {
            convertView.setEnabled(false);
        } else if ((Calendar.getInstance().get(Calendar.YEAR) - entity.getYear() == 0) && ((Calendar.getInstance().get(Calendar.MONTH) + 1) - (entity.getMonth()) == 0) && (Calendar.getInstance().get(Calendar.DATE) < entity.getDay())) {
            convertView.setEnabled(false);
        } else {
            convertView.setEnabled(true);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (entity.getDreamType() == -1) {
                        KeyCounterUtils.SendKeyCounter("93e71143d9cb495c828998d50bb9d499");
                        Intent intent = new Intent(mContext, RecordDreamTitleActivcity.class);
                        intent.putExtra("key_history_time",entity.toString());
                        mContext.startActivity(intent);
                    } else {
                        Intent intent = new Intent(mContext, PersonalInfoActivity.class);
                        intent.putExtra("userId", PreferenceUtils.GetUserId());
                        intent.putExtra("Day", entity.toString());
                        mContext.startActivity(intent);
                    }
                }
            });
        }

    }

    public void setDatas(List<GridCalenderDateEntity> sources) {
        if (sources != null) {
            datas.clear();
            datas.addAll(sources);
            notifyDataSetChanged();
        }
    }
}
